home *** CD-ROM | disk | FTP | other *** search
- /*------------------------------misc.c--------------------------------------*/
- /*
- Copyright 1992 David Conger
- */
-
-
-
- /*---------------------------include files----------------------------------*/
-
- #include <bios.h>
- #include "misc.h"
- #include <time.h>
- #include <stdarg.h>
- #include <graphics.h>
- #include <dos.h>
- #include <stdio.h>
- #include "game.h"
-
- /*-------------------------end include files--------------------------------*/
-
-
-
-
- /*---------------------------local constants--------------------------------*/
-
- #define ASCII_CHAR_MASK 0x00FF
-
- #define TICKS_PER_SECOND 1024
- #define SECONDS_PER_MINUTE 60
- #define MINUTES_PER_HOUR 60
-
- #define INTERRUPT_70H 0x70
- #define INTERRUPT_15H 0x15
- #define SET_REAL_TIME_CLOCK_INTERVAL 0x83
- #define TIMER_REMOVAL_VALIDATION_DELAY 200
- #define REAL_TIME_INTERRUPT_COUNT 0xFFFF
- #define REAL_TIME_INTERRUPT_POKE_SEGMENT 0x40
- #define REAL_TIME_INTERRUPT_POKE_OFFSET 0x9C
- #define ARBITRARY_VALUE 0x500
-
- /*-------------------------end local constants------------------------------*/
-
-
-
-
- /*----------------------------module globals--------------------------------*/
-
- game_timer time_since_initialization={0};
- static unsigned timer_flags;
- static void interrupt (*old_interrupt_vector)();
-
- /*--------------------------end module globals------------------------------*/
-
-
-
- /*----------------------------local prototypes------------------------------*/
-
- void interrupt timer_interrupt_handler(void);
-
- /*--------------------------end local prototypes----------------------------*/
-
-
-
-
-
- /*---------------------------get_keystroke----------------------------------*/
-
- int get_keystroke(int pause,int *special_key)
- {
- int key;
-
-
- *special_key=0;
- key=bioskey(1);
-
- if ((!key) && (pause==WAIT))
- {
- while (!bioskey(1))
- /* wait */;
- key=1;
- }
-
- if (key)
- {
- key=bioskey(0);
-
- if ((key & ASCII_CHAR_MASK)==0)
- {
- *special_key=(key>>8);
- key=0;
- }
- else
- {
- *special_key=0;
- key&=0x00FF;
- }
- }
-
- return(key);
- }
-
- /*-------------------------end get_keystroke--------------------------------*/
-
-
-
-
-
- /*--------------------------------wait--------------------------------------*/
-
- void wait(int seconds)
- {
- time_t start_time,current_time;
-
- time(&start_time);
- do
- {
- time(¤t_time);
-
- } while (difftime(current_time,start_time)<seconds);
- }
-
- /*------------------------------end wait------------------------------------*/
-
-
-
-
-
- /*------------------------------write_message--------------------------------*/
-
- void write_message(int row,int col,char *format_str,...)
- {
- va_list arg_list;
- char message[MAX_MESSAGE_LEN];
-
-
- moveto(col,row);
- va_start(arg_list,format_str);
- vsprintf(message,format_str,arg_list);
- va_end(arg_list);
- outtextxy(col,row,message);
- }
-
- /*----------------------------end write_message------------------------------*/
-
-
-
-
- /*---------------------------install_timer----------------------------------*/
-
- boolean install_timer(void)
- {
- struct SREGS sreg;
- union REGS inregs,outregs;
- unsigned far *flag_ptr;
-
-
- old_interrupt_vector=getvect(INTERRUPT_70H);
- setvect(INTERRUPT_70H,timer_interrupt_handler);
-
- flag_ptr=(unsigned far *)(&timer_flags);
- sreg.es=FP_SEG(flag_ptr);
- inregs.x.bx=FP_OFF(flag_ptr);
- inregs.h.ah=SET_REAL_TIME_CLOCK_INTERVAL;
- inregs.h.al=0;
- inregs.x.cx=ARBITRARY_VALUE;
- inregs.x.dx=0;
- int86x(INTERRUPT_15H,&inregs,&outregs,&sreg);
-
- return(outregs.x.cflag);
- }
-
- /*-------------------------end install_timer--------------------------------*/
-
-
-
-
- /*----------------------------remove_timer----------------------------------*/
-
- boolean remove_timer(void)
- {
- disable();
- setvect(INTERRUPT_70H,old_interrupt_vector);
- delay(TIMER_REMOVAL_VALIDATION_DELAY);
- enable();
-
- return(!timer_flags);
- }
-
- /*--------------------------end remove_timer--------------------------------*/
-
-
-
-
-
- /*-----------------------timer_interrupt_handler----------------------------*/
-
- void interrupt timer_interrupt_handler(void)
- {
- poke(REAL_TIME_INTERRUPT_POKE_SEGMENT,REAL_TIME_INTERRUPT_POKE_OFFSET,
- REAL_TIME_INTERRUPT_COUNT);
-
- time_since_initialization.ticks++;
- if (time_since_initialization.ticks>TICKS_PER_SECOND)
- {
- time_since_initialization.ticks-=TICKS_PER_SECOND;
-
- time_since_initialization.seconds++;
- if (time_since_initialization.seconds>SECONDS_PER_MINUTE)
- {
- time_since_initialization.seconds-=SECONDS_PER_MINUTE;
-
- time_since_initialization.minutes++;
- if (time_since_initialization.minutes>MINUTES_PER_HOUR)
- {
- time_since_initialization.minutes-=MINUTES_PER_HOUR;
- time_since_initialization.hours++;
- }
- }
- }
- if (old_interrupt_vector!=NULL)
- (*old_interrupt_vector)();
- }
-
- /*---------------------end timer_interrupt_handler--------------------------*/
-
-
-
-
- /*-----------------------------set_timer------------------------------------*/
-
- void set_timer(game_timer *current_time)
- {
- current_time->ticks=time_since_initialization.ticks;
- current_time->seconds=time_since_initialization.seconds;
- current_time->minutes=time_since_initialization.minutes;
- current_time->hours=time_since_initialization.hours;
- }
-
- /*---------------------------end set_timer----------------------------------*/
-
-
-
- /*-----------------------------elapsed_time---------------------------------*/
-
- game_time elapsed_time(game_timer *start_time)
- {
- game_timer temp,current_time;
- game_time answer;
-
-
- current_time=time_since_initialization;
-
- temp.ticks=current_time.ticks - start_time->ticks;
- if (temp.ticks<0)
- {
- current_time.seconds--;
- temp.ticks+=TICKS_PER_SECOND;
- }
-
- temp.seconds=current_time.seconds - start_time->seconds;
- if (temp.seconds<0)
- {
- current_time.minutes--;
- temp.seconds+=SECONDS_PER_MINUTE;
- }
-
- temp.minutes=current_time.minutes - start_time->minutes;
- if (temp.minutes<0)
- {
- current_time.hours--;
- temp.seconds+=MINUTES_PER_HOUR;
- }
-
- temp.hours=current_time.hours - start_time->hours;
-
- answer=temp.hours * MINUTES_PER_HOUR * SECONDS_PER_MINUTE *
- TICKS_PER_SECOND;
-
- answer+=temp.minutes * SECONDS_PER_MINUTE * TICKS_PER_SECOND;
-
- answer+=temp.seconds * TICKS_PER_SECOND;
-
- answer+=temp.ticks;
-
- return(answer);
- }
-
- /*---------------------------end elapsed_time-------------------------------*/
-
-
- /*----------------------------end misc.c------------------------------------*/
-
-